package com.geeguo.ebuilder.client.base.tenant.utils;

import com.geeguo.ebuilder.client.base.tenant.model.DataContext;

public class DataContextHolder {

    /**
     * 用于存储线程本地变量的ThreadLocal实例
     */
    private static final ThreadLocal<DataContext> DATA_CONTEXT_THREAD_LOCAL = new ThreadLocal<>();

    /**
     * 设置当前线程的数据访问上下文
     *
     * @param dataContext 数据访问上下文
     */
    public static void setDataContext(DataContext dataContext) {
        DATA_CONTEXT_THREAD_LOCAL.set(dataContext);
    }

    /**
     * 获取当前线程的数据访问上下文
     *
     * @return 当前线程的数据访问上下文，如果未设置则返回null
     */
    public static DataContext getDataContext() {
        return DATA_CONTEXT_THREAD_LOCAL.get();
    }

    /**
     * 清除当前线程的数据访问上下文
     * 建议在请求结束时调用，防止内存泄漏和数据混淆
     */
    public static void clearDataContext() {
        DATA_CONTEXT_THREAD_LOCAL.remove();
    }
}
